<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:nydml="http://www.nourysolutions.com/salamanca/DML" exclude-result-prefixes="nydml">
  <xsl:output method="text" encoding="UTF-8"/>

  <xsl:param name="name" />

  <xsl:include href="functions.xslt" />

  <xsl:template match="project" mode="project-declaration">
    <xsl:text>
Project("</xsl:text>
    <xsl:choose>
      <xsl:when test="@type='sql'">
        <xsl:text>{4F174C21-8C12-11D0-8340-0000F80270F8}</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text>") = "</xsl:text>
    <xsl:value-of select="@name" />
    <xsl:text>", "</xsl:text>
    <xsl:apply-templates select="." mode="path-from-root">
      <xsl:with-param name="root" select="ancestor::solution[1]" />
    </xsl:apply-templates>
    <xsl:value-of select="@name" />
    <xsl:choose>
      <xsl:when test="@type='sql'">
        <xsl:text>.dbp</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>.csproj</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text>", "</xsl:text>
    <xsl:value-of select="@id" />
    <xsl:text>"
EndProject</xsl:text>

  </xsl:template>

  <xsl:template match="project" mode="project-configuration-core">
    <xsl:param name="mode" />
    <xsl:variable name="id" select="@id" />
    <xsl:variable name="projectMode" select="@mode" />
    <xsl:for-each select="/manifest/configuration/platform">
      <xsl:text>
		</xsl:text>
      <xsl:value-of select="$id" />
      <xsl:text>.</xsl:text>
      <xsl:value-of select="$mode" />
      <xsl:text>|</xsl:text>
      <xsl:value-of select="@name" />
      <xsl:text>.ActiveCfg = </xsl:text>
      <xsl:value-of select="$mode" />
      <xsl:text>|</xsl:text>
      <xsl:value-of select="@name" />

      <xsl:if test="string-length($projectMode)=0 or $projectMode=@mode">
        <xsl:text>
		</xsl:text>
        <xsl:value-of select="$id" />
        <xsl:text>.</xsl:text>
        <xsl:value-of select="$mode" />
        <xsl:text>|</xsl:text>
        <xsl:value-of select="@name" />
        <xsl:text>.Build.0 = </xsl:text>
        <xsl:value-of select="$mode" />
        <xsl:text>|</xsl:text>
        <xsl:value-of select="@name" />
      </xsl:if>
    </xsl:for-each>
  </xsl:template>


  <xsl:template match="project" mode="project-configuration">
    <xsl:apply-templates select="." mode="project-configuration-core">
      <xsl:with-param name="mode" select="'Debug'" />
    </xsl:apply-templates>
    <xsl:apply-templates select="." mode="project-configuration-core">
      <xsl:with-param name="mode" select="'Release'" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template name="solution-configuration">
    <xsl:param name="mode" />
    <xsl:for-each select="/manifest/configuration/platform">
      <xsl:text>
		</xsl:text>
      <xsl:value-of select="$mode" />
      <xsl:text>|</xsl:text>
      <xsl:value-of select="@name" />
      <xsl:text> = </xsl:text>
      <xsl:value-of select="$mode" />
      <xsl:text>|</xsl:text>
      <xsl:value-of select="@name" />
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="/">
    <xsl:text>
Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008</xsl:text>
    <xsl:apply-templates select="//solution[@name=$name]//project" mode="project-declaration" />
    <xsl:text>
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution</xsl:text>
    <xsl:call-template name="solution-configuration">
      <xsl:with-param name="mode" select="'Debug'" />
    </xsl:call-template>
    <xsl:call-template name="solution-configuration">
      <xsl:with-param name="mode" select="'Release'" />
    </xsl:call-template>
    <xsl:text>
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution</xsl:text>
    <xsl:apply-templates select="//solution[@name=$name]//project[not(@type='sql')]" mode="project-configuration" />
    <xsl:text>
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
EndGlobal
</xsl:text>
  </xsl:template>
</xsl:stylesheet>
